package com.yoomiito.app.ui.fragment.my;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yoomiito.app.R;
import com.yoomiito.app.base.BaseLazyFragment;
import com.yoomiito.app.utils.DensityUtil;
import com.yoomiito.app.utils.ResUtil;
import com.yoomiito.app.widget.SpacesItemDecoration;
import com.yoomiito.app.widget.state.ContentType;
import com.yoomiito.app.widget.state.EmptyType;
import com.yoomiito.app.widget.state.StatusManager;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.mvp.IPresent;

/**
 * 首页
 */
public abstract class MyOrderBaseFragment<P extends IPresent> extends BaseLazyFragment<P> {

    @BindView(R.id.fm_rcy)
    protected RecyclerView mRecyclerView;
    @BindView(R.id.fm_refreshLayout)
    protected SmartRefreshLayout mSmartRefreshLayout;

    protected int currentPage = 1;
    protected long minId = 1;
    private View mTipView;
    private PopupWindow mPopupWindow;

    @Override
    public int getLayoutId() {
//        return R.layout.fragment_my_order_base;
        return 0;
    }

    @Override
    public View getLayoutView() {
        mStatusManager = new StatusManager.Builder(context)
                .addType(new ContentType(R.layout.fm_rcy))
                .addType(new EmptyType().setEmptyContent(getEmptyContent()))
                .build();
        return mStatusManager.view();
    }

    @Override
    public void initData(Bundle savedInstanceState) {
        if (!useMyItemDecoration()) {
//            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
//            mRecyclerView.addItemDecoration(new MyItemDecoration(context, RecyclerView.VERTICAL));
//            mRecyclerView.setLayoutManager(linearLayoutManager);

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            mRecyclerView.addItemDecoration(new SpacesItemDecoration(ResUtil.getSize(R.dimen.dp_10)));
            mRecyclerView.setLayoutManager(linearLayoutManager);
        }

        mSmartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            currentPage = 1;
            minId = 1;
            getNewData();
        });

        mSmartRefreshLayout.setOnLoadMoreListener(refreshLayout -> {
            currentPage++;
            getNewData();
        });
        if (!isRefresh()){
            mSmartRefreshLayout.setEnableLoadMore(false);
            mSmartRefreshLayout.setEnableRefresh(false);
        }else {
            mSmartRefreshLayout.autoRefresh();
        }
    }

    protected boolean useMyItemDecoration(){
        return false;
    }

    protected abstract void getNewData();

    public abstract String getEmptyContent();

    public boolean isRefresh(){
        return true;
    }


    public void showTip(View view, String tip, int type){
        if (mTipView==null){
            mTipView = LayoutInflater.from(context).inflate(R.layout.small_tip_2,null,false);
        }
        TextView tipTv = mTipView.findViewById(R.id.tip);
        ImageView iv = mTipView.findViewById(R.id.indicatorIv);
        tipTv.setText(tip);
        if (mPopupWindow==null){
            mPopupWindow = new PopupWindow(context);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setContentView(mTipView);
            mPopupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00000000")));
        }
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int viewX = location[0];
        int viewY = location[1];
        int viewW = view.getWidth();
        int viewH = view.getHeight();
//        int popupWindowWidth = DensityUtil.dp2px(190);
        int x =0,y=0;
//        if (viewX>popupWindowWidth/2){//
//            if ((App.mWidthPx-viewW-viewX)>popupWindowWidth/2){
//                x = viewX+viewW/2-popupWindowWidth/2;
//                y = viewY+ viewH+DensityUtil.dp2px(5);
//            }else {
//
//            }
//        }else {
//
//        }
        if (type==1){
            x = viewX+viewW/2- DensityUtil.dp2px(14);
            y = viewY+ viewH+DensityUtil.dp2px(5);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(10);
            iv.setLayoutParams(lp);
        }else if (type==2){
            x = viewX+viewW/2-DensityUtil.dp2px(170);
            y = viewY+ viewH+DensityUtil.dp2px(5);
            LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) iv.getLayoutParams();
            lp.leftMargin = DensityUtil.dp2px(166);
            iv.setLayoutParams(lp);
        }
        mPopupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y);
    }
}
